package com.doge.calcalbum.business.note

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.doge.calcalbum.R
import com.doge.calcalbum.business.ITEM_CHECK
import com.doge.calcalbum.core.base.adapters.AdapterCallback
import com.doge.calcalbum.core.base.adapters.ITEM_CLICKED
import com.doge.calcalbum.core.base.adapters.ITEM_LONG_CLICKED
import com.doge.calcalbum.databinding.CellNoteBinding
import com.doge.calcalbum.db.model.Note
import com.doge.calcalbum.utils.setOnClickCallback
import com.doge.calcalbum.utils.timeDesc

class NoteViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    val binding: CellNoteBinding = CellNoteBinding.inflate(
        LayoutInflater.from(parent.context),
        parent,
        false
    ),
) : RecyclerView.ViewHolder(binding.root) {


    init {
        itemView.setOnClickCallback {
            callback.invoke(ITEM_CLICKED, itemView.tag)
        }
        itemView.setOnLongClickListener {
            callback.invoke(ITEM_LONG_CLICKED, itemView.tag)
            true
        }
        binding.checkbox.setOnClickListener {
            callback.invoke(ITEM_CHECK, itemView.tag)
        }
    }

    fun bind(item: Note) {
        itemView.tag = item
        binding.title.text = item.title
        binding.time.text = item.createTime.timeDesc(format = "yyyy年MM月dd日 HH:mm:ss")
        binding.checkbox.isVisible = item.selected != null
        binding.checkbox.tag = item.selected
        binding.checkbox.setImageResource(
            if (item.selected == true) R.mipmap.check_selected else R.mipmap.check_normal_transparent
        )
    }
}